% Copyright (C) 2019-2023 Benjamin Born, Francesco D'Ascanio, Gernot J. Mueller, Johannes Pfeifer
%
% This is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% It is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% For a copy of the GNU General Public License,
% see <http://www.gnu.org/licenses/>.

%% Generate Figure 2: Quantitative model irf

loaded_IRFs.float=load('IRF_results/irfs_float.mat');
loaded_IRFs.peg=load('IRF_results/irfs_peg.mat');
loaded_IRFs.peg_ergodic=load('IRF_results/irfs_peg_slack.mat');

ending_cell={'float','peg','peg_ergodic'
    'Float','Peg (no slack)','Peg (slack)'};

mycolors = [0.9290    0.6940    0.1250; 0.4940    0.1840    0.5560];

IRF_length=8;
h=figure('Name','IRF Overview');
set(h,'PaperType','a4','PaperPositionMode','manual','PaperUnits','centimeters','PaperPosition',[1,1,28,21],'renderer', 'painters');
colormap(mycolors);
for ending_iter=1:size(ending_cell,2)
    subplot(3,3,ending_iter)
    plot(1:IRF_length,zeros(1,IRF_length),'k:','LineWidth',0.5)
    hold on
    hh=plot(1:IRF_length,100*loaded_IRFs.(ending_cell{1,ending_iter}).IRFs(loaded_IRFs.(ending_cell{1,ending_iter}).pos.y_N,1:IRF_length,1),'b-',...
        1:IRF_length,100*loaded_IRFs.(ending_cell{1,ending_iter}).IRFs(loaded_IRFs.(ending_cell{1,ending_iter}).pos.y_N,1:IRF_length,2),'r--','LineWidth',2);
    xlim([1 IRF_length])
    ylim([-2.5 2.5])
    title(ending_cell{2,ending_iter},'Fontsize',12,'interpreter','latex');
    ax = gca;
    ax.YAxis.Exponent = 0;
    ax.FontSize=12;
    if ending_iter==1
        ll=legend(hh,'gov. spending increase','gov. spending cut');
        ll.Box='off';
        ll.Location='SouthEast';
        ylabel([loaded_IRFs.(ending_cell{1,ending_iter}).var_name{loaded_IRFs.(ending_cell{1,ending_iter}).pos.y_N,2} ' (\% of st.st.)'],...
            'interpreter','latex','FontSize',12)
    end
end

for ending_iter=1:size(ending_cell,2)
    subplot(3,3,3+ending_iter)
    plot(1:IRF_length,zeros(1,IRF_length),'k:','LineWidth',0.5)
    hold on
    plot(1:IRF_length,100*loaded_IRFs.(ending_cell{1,ending_iter}).IRFs(loaded_IRFs.(ending_cell{1,ending_iter}).pos.rer,1:IRF_length,1)./loaded_IRFs.(ending_cell{1,ending_iter}).Y_base_mean(loaded_IRFs.(ending_cell{1,ending_iter}).pos.rer),'b-',...
        1:IRF_length,100*loaded_IRFs.(ending_cell{1,ending_iter}).IRFs(loaded_IRFs.(ending_cell{1,ending_iter}).pos.rer,1:IRF_length,2)./loaded_IRFs.(ending_cell{1,ending_iter}).Y_base_mean(loaded_IRFs.(ending_cell{1,ending_iter}).pos.rer),'r--','LineWidth',2);
    xlim([1 IRF_length])
    ylim([-6 6])
    ax = gca;
    ax.YAxis.Exponent = 0;
    ax.FontSize=12;
    xlabel('quarters')
    if ending_iter==1
        ylabel([loaded_IRFs.(ending_cell{1,ending_iter}).var_name{loaded_IRFs.(ending_cell{1,ending_iter}).pos.rer,2} ' (\% of erg. mean)'],...
            'interpreter','latex','FontSize',12)
    end
end

for ending_iter=1:size(ending_cell,2)
    subplot(3,3,6+ending_iter)
    plot(1:IRF_length,zeros(1,IRF_length),'k:','LineWidth',0.5)
    hold on
    hh=plot(1:IRF_length,100*loaded_IRFs.(ending_cell{1,ending_iter}).IRFs(loaded_IRFs.(ending_cell{1,ending_iter}).pos.g_N,1:IRF_length,1),'b-',...
        1:IRF_length,100*loaded_IRFs.(ending_cell{1,ending_iter}).IRFs(loaded_IRFs.(ending_cell{1,ending_iter}).pos.g_N,1:IRF_length,2),'r--','LineWidth',2);
    xlim([1 IRF_length])
    ylim([-3 3])
    ax = gca;
    ax.YAxis.Exponent = 0;
    ax.FontSize=12;
    xlabel('quarters')
    if ending_iter==1
        ylabel([loaded_IRFs.(ending_cell{1,ending_iter}).var_name{loaded_IRFs.(ending_cell{1,ending_iter}).pos.g_N,2} ' (p.p.)'],...
            'interpreter','latex','FontSize',12)
    end
end

print('IRF_results/IRFs_paper','-depsc2')
saveas(h,'IRF_results/IRFs_paper');

%Do trade balance
h=figure('Name','IRF Overview TB');
set(h,'PaperType','a4','PaperPositionMode','manual','PaperUnits','centimeters','PaperPosition',[1,1,28,21],'renderer', 'painters');
colormap(mycolors);
for ending_iter=1:size(ending_cell,2)
    subplot(3,3,ending_iter)
    plot(1:IRF_length,zeros(1,IRF_length),'k:','LineWidth',0.5)
    hold on
    hh=plot(1:IRF_length,100*loaded_IRFs.(ending_cell{1,ending_iter}).IRFs(loaded_IRFs.(ending_cell{1,ending_iter}).pos.trade_balance,1:IRF_length,1),'b-',...
        1:IRF_length,100*loaded_IRFs.(ending_cell{1,ending_iter}).IRFs(loaded_IRFs.(ending_cell{1,ending_iter}).pos.trade_balance,1:IRF_length,2),'r--','LineWidth',2);
    xlim([1 IRF_length])
    ylim([-1.0 1.0])
    title(ending_cell{2,ending_iter},'Fontsize',12,'interpreter','latex');
    ax = gca;
    ax.YAxis.Exponent = 0;
    ax.FontSize=12;
end
print('IRF_results/IRFs_paper_TB','-depsc2')
saveas(h,'IRF_results/IRFs_paper_TB');

clear; close all
mycolors = [0.9290    0.6940    0.1250; 0.4940    0.1840    0.5560];
% name_vec = {'FX_peg', 'FX_intermediate', 'FX_float','Y_peg', 'Y_intermediate', 'Y_float'};
name_vec = {'G_peg_ergodic', 'G_peg', 'G_float','FX_peg_ergodic', 'FX_peg', 'FX_float', 'Y_peg_ergodic', 'Y_peg', 'Y_float'};
ff=openfig('IRF_results/IRFs_paper.fig');
axesH = findobj(ff,'Type','axes','-not','Tag','legend'); % This will change the order from which the axes will be saved. If you need to save in the correct order, you will need access to the subplot handles
nAxes = numel(axesH);
newFig = figure;
for k=1:nAxes
    newAxes=copyobj(axesH(k),newFig);
    % Expand subplot to occupy the hole figure:
    set(newAxes,'OuterPosition',[0 0 1 1]);
    set(newAxes,'Ylabel',[]);
    set(newAxes,'Xlabel',[]);
    set(newAxes,'Title',[]);
    set(newAxes,'FontSize', 20);
    set(newAxes,'LineWidth', 2);
    temp=findall(newAxes.Children,'-property','LineWidth');
    set(temp(end),'LineWidth',2)
    set(temp(1:2),'LineWidth',5)
    set(newAxes,'XTick',[1:8])
    set(newAxes,'TickLabelInterpreter','latex')
    tightInset=get(newAxes,'TightInset');
    set(newAxes,'Position',[tightInset(1:2) [1 1]-(tightInset(3:4)+tightInset(1:2))])
    set(findall(newFig,'-property','ShowBaseLine'),'ShowBaseLine','off')
    if k==nAxes
        legend(newAxes.Children([2,1]),'gov. spending increase','gov. spending cut','box','off','Location','NorthEast')
    end
    print(newFig, ['Figures/model_IRF_',name_vec{k}, '.pdf'],'-dpdf')
    delete(newAxes);
end
delete(newFig);


name_vec = {'TB_peg_ergodic', 'TB_peg', 'TB_float'};
ff=openfig('IRF_results/IRFs_paper_TB.fig');
axesH = findobj(ff,'Type','axes','-not','Tag','legend'); % This will change the order from which the axes will be saved. If you need to save in the correct order, you will need access to the subplot handles
nAxes = numel(axesH);
newFig = figure;
for k=1:nAxes
    newAxes=copyobj(axesH(k),newFig);
    % Expand subplot to occupy the hole figure:
    set(newAxes,'OuterPosition',[0 0 1 1]);
    set(newAxes,'Ylabel',[]);
    set(newAxes,'Xlabel',[]);
    set(newAxes,'Title',[]);
    set(newAxes,'FontSize', 20);
    set(newAxes,'LineWidth', 2);
    temp=findall(newAxes.Children,'-property','LineWidth');
    set(temp(end),'LineWidth',2)
    set(temp(1:2),'LineWidth',5)
    set(newAxes,'XTick',[1:8])
    set(newAxes,'TickLabelInterpreter','latex')
    tightInset=get(newAxes,'TightInset');
    set(newAxes,'Position',[tightInset(1:2) [1 1]-(tightInset(3:4)+tightInset(1:2))])
    set(findall(newFig,'-property','ShowBaseLine'),'ShowBaseLine','off')
    print(newFig, ['Figures/model_IRF_',name_vec{k}, '.pdf'],'-dpdf')
    delete(newAxes);
end
delete(newFig);